/**
 * 
 */
package org.arpit.masteringextjs.rs;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.arpit.masteringextjs.dao.impl.GroupsDAOImpl;
import org.arpit.masteringextjs.domain.Groups;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author arpit
 *
 */
@Path("/json/groups")
public class GroupsResource {

	@GET
	@Path("/findAll")
	@Produces(MediaType.APPLICATION_JSON)
	public String findAll() throws JsonGenerationException, JsonMappingException, IOException {
		System.out.println("Inside GroupsResource findAll!!");
		List<Groups> groups = GroupsDAOImpl.getInstance().findAll();
		
		ObjectMapper mapper = new ObjectMapper();
		String json = "";

		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("data", groups);

		// convert map to JSON string
		json = mapper.defaultPrettyPrintingWriter().writeValueAsString(map);
		//System.out.println(json);
		return json;
	}
	
	public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
		new GroupsResource().findAll();
	}
}
